package com.heima.common.tess4j;

import lombok.Data;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @ClassName Tess4jClient
 * @Description TODO
 * @Author kiku
 * @Date 2024/8/27 20:01
 * @Version 1.0
 */
@Data
@Component
@ConfigurationProperties(prefix = "tess4j")
public class Tess4jClient {
    private String dataPath="D:\\tessdata";
    private String language="chi_sim";
    public String doOCR(BufferedImage image) throws TesseractException {
        //创建实例
        ITesseract tesseract = new Tesseract();

        //设置字体库路径
        tesseract.setDatapath(dataPath);

        //设置语言 -->简体中文
        tesseract.setLanguage(language);

        File file = new File("D:\\143.png");

        //识别图片
        String result = tesseract.doOCR(file);

        System.out.println("识别的结果为："+result.replaceAll("\\r|\\n","-"));

        return result;
    }
}
